package com.jblog.common;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate4.HibernateTemplate;
import org.springframework.stereotype.Repository;

import com.jblog.user.model.User;

@Repository
public class BaseDaoImpl<T> implements BaseDao<T> {

	private Class<T> clazz;
	
	@Autowired
	HibernateTemplate ht;
	
	@SuppressWarnings("unchecked")
	public BaseDaoImpl() {
        Type t = getClass().getGenericSuperclass();
        if (t instanceof ParameterizedType) {
            Type[] p = ((ParameterizedType) t).getActualTypeArguments();
            this.clazz = (Class<T>) p[0];
        }
	}

	@Override
	public void add(T t) {
		ht.save(t);
	}

	@Override
	public void update(T t) {
		ht.update(t);
	}

	@Override
	public void delete(T t) {
		ht.delete(t);
	}

	@Override
	public T find(Serializable id) {
		return (T)ht.get(clazz, id);
	}

	@Override
	public void merge(T t) {
		ht.merge(t);
	}
	
	public static void main(String[] args) {
		BaseDao<User> baseDao = new BaseDaoImpl<User>();
		
	}
	
}
